package com.yuju.group.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.SeqCodeEnum;
import com.yuju.enums.StatusEnum;
import com.yuju.exception.BusinessException;
import com.yuju.group.dao.IGroupDao;
import com.yuju.group.dao.entity.Group;
import com.yuju.group.enums.GroupReturnCodeEnum;
import com.yuju.group.enums.HotelReturnCodeEnum;
import com.yuju.group.enums.SeqCodeReturnCodeEnum;
import com.yuju.group.model.GroupModel;
import com.yuju.group.model.HotelModel;
import com.yuju.group.service.ICodeGenService;
import com.yuju.group.service.IGroupParamService;
import com.yuju.group.service.IGroupService;
import com.yuju.group.service.IHotelService;
import com.yuju.group.service.ISeqCodeService;
import com.yuju.util.Utils;
/**
 * 
 * 类名：GroupServiceImpl  <br />
 *
 * 功能：
 *
 * @author : sunyl <br />
 * @Date : 2017年11月24日 上午9:37:25  <br />
 * @version : 1.1 <br />
 */
@Service
public class GroupServiceImpl extends AbstractBaseService<GroupModel,Group,String> implements IGroupService{
	@Autowired
	private IGroupDao groupDao;
	@Autowired
	private ISeqCodeService seqCodeService;
	@Autowired
	private IHotelService hotelService;
	@Autowired
	private ICodeGenService codeGenService;
	
	@Autowired
	private IGroupParamService groupParamService;
	
	@Override
	public IBaseDao<GroupModel, Group, String> getDao() {
		// TODO Auto-generated method stub
		return groupDao;
	}
	@Override
	public int save(GroupModel model) {
		/*重名验证*/
		if(!validRepeatName(model.getId(), model.getName())){
			throw new BusinessException(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode(),
					ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		/*集团id*/
		String id = Utils.getUUID();
		String code = codeGenService.generate(null, null, null,SeqCodeEnum.GROUP_CODE.getCode());
		if(code == null){
			throw new BusinessException(SeqCodeReturnCodeEnum.SEQ_GROUP_SAVE_FAILURE.getCode(),
					SeqCodeReturnCodeEnum.SEQ_GROUP_SAVE_FAILURE.getDesc());
		}
		// 保存集团
		model.setId(id);
		model.setCode(code);
		model.setGmtCreate(new Date());
		model.setGmtModified(new Date());
		int rows = super.save(model);
		if(!seqCodeService.initGroupSeqCode(id)){
			throw new BusinessException
			(SeqCodeReturnCodeEnum.SEQ_GROUP_SAVE_FAILURE.getCode(),
					SeqCodeReturnCodeEnum.SEQ_GROUP_SAVE_FAILURE.getDesc());
		}
		/*把农村集团酒店*/
		HotelModel hotelModel = new HotelModel();
		hotelModel.setName(model.getName());
		hotelModel.setIsGroup(StatusEnum.TRUE.getValue());
		hotelModel.setAddress(model.getAddress());
		hotelModel.setPhone(model.getPhone());
		hotelModel.setReservePhone(model.getReservePhone());
		hotelModel.setGroupId(id);
		hotelModel.setInfo(model.getInfo());
		if(hotelService.save(hotelModel)==0){
			throw new BusinessException(HotelReturnCodeEnum.Hotel_SAVE_FAILURE.getCode(), 
					HotelReturnCodeEnum.Hotel_SAVE_FAILURE.getDesc());
		}
		
		// 初始化集团参数
		groupParamService.initGroupParam(id);
		
		return rows;
	}
	@Override
	public int updateSelective(GroupModel model) {
		/*重名验证*/
		if(!validRepeatName(model.getId(), model.getName())){
			throw new BusinessException(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode(),
					ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
		}
		/**
		 * 更新酒店是集团的分店姓名
		 */
		if(hotelService.updateSelectiveForUnValidForGroupName(model.getId(),model.getName())==0){
			throw new BusinessException(HotelReturnCodeEnum.Hotel_UPDATE_FAILURE.getCode(),
					HotelReturnCodeEnum.Hotel_UPDATE_FAILURE.getDesc());
		}
		// 更新数据
		model.setGmtCreate(new Date());
		return super.updateSelective(model);
	}
	/**
	 * 更新酒店的分店数量
	 * 功能：<br/>
	 *
	 * @author sunyl
	 * @Date 2017年12月18日 下午2:06:29 <br/>
	 */
	@Override
	public int updateForHotelCount(String groupId) {
		HotelModel hotelModel = new HotelModel();
		hotelModel.setGroupId(groupId);
		hotelModel.setIsEnable(StatusEnum.TRUE.getValue());
		hotelModel.setIsGroup(StatusEnum.FALSE.getValue());
		List<HotelModel> finds = hotelService.list(hotelModel);
		GroupModel groupModel = new GroupModel();
		groupModel.setId(groupId);
		groupModel.setGmtCreate(new Date());
		if(finds != null){
			groupModel.setHotelCount(finds.size());
		}else{
			groupModel.setHotelCount(0);
		}
		// 更新数据
		return super.updateSelective(groupModel);
	}
	/**
     * 重命名验证
     * 功能：<br/>
     *
     * @author sunyl
     * @Date 2017年12月21日 下午8:00:32 <br/>
     */
	@Override
	public boolean validRepeatName(String id, String name){
		if(id != null){
			GroupModel groupModel = super.findById(id);
			if(groupModel == null){
				throw new BusinessException(GroupReturnCodeEnum.GROUP_VALIDNAME_FAILURE.getCode(),
						GroupReturnCodeEnum.GROUP_VALIDNAME_FAILURE.getDesc());
			}
			if(groupModel.getName().equals(name)){
				return true;
			}else{
				return validRepeatNameForNullId(name);
			}
		}else{
			return validRepeatNameForNullId(name);
		}
	};
	private boolean validRepeatNameForNullId(String name){
		GroupModel groupModel = new GroupModel();
		groupModel.setName(name);
		GroupModel find = super.find(groupModel);
		if(find == null){
			return true ;
		}else{
			return false;
		}
	}
}
